package com.boxin.ims.modules.wechat.busi;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;

/**
 * @author Jakemanse
 * @date 2013-10-6
 * @function <p>现场抽次后台 回复‘现场抽奖 获取奖品凭证’ </p>
 */
@Service
public class SceneLotteryProcessor  extends BusinessProcessor{
	
	@Autowired
	WechatDaoHelper wechatDaoHelper;
	
	
	
	public SceneLotteryProcessor(){
		super();
	}

	@Override
	public ProcessMessage process() {
		
		WechatMember member = memberCheckAndReply();
		String reply = "亲，对不起，未找到您的获奖信息，谢谢！";
		if (member != null) {//
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("memberId", member.getId());
			params.put("userId", user.getId());
			List<Map<String, Object>> result = (List<Map<String, Object>>) wechatDaoHelper
					.querySellerLotteryRecordByMember(params);
			if (result != null && !result.isEmpty()) {
				if (result.size() == 1) {
					Map map = result.get(0);
					String title = "您有一份未使用的奖品【" + result.get(0).get("name")
							+ "】";
					Date date = (Date) map.get("end_time");
					String validDate = DateUtils.formatDate(
							(date == null) ? new Date() : date, "MM月dd日");
					String desc = "亲，您有一份未使用的奖品【" + map.get("name") + "】,请于"
							+ validDate + "之前到店里凭此二维码核销使用，过期作废，谢谢！";
					writeSingleLotteryImageToUser(title, desc,
							Long.parseLong(map.get("qrcode_id").toString()));
					return null;
				} else {	//多条中奖信息
					List<NewsReply> newsList = new ArrayList<NewsReply>();
					for (Map<String, Object> row : result) {
						NewsReply newsReply = new NewsReply();
						Date date = (Date) row.get("end_time");
						String validDate = DateUtils.formatDate(
								(date == null) ? new Date() : date, "MM月dd日");
						newsReply.setTitle("您有未核销的奖品【"+row.get("name").toString()+"】");
						newsReply.setDescription("您有未核销的奖品【"
								+ row.get("name").toString()+"】请于"+validDate+"之前到店里凭此打开的二维码到服务台核销使用,谢谢!");
						newsReply.setPicUrl(QRCodeUtils.getQRCodePNGURL(Long
								.parseLong(row.get("qrcode_id").toString())));
						newsReply.setUrl(QRCodeUtils.getQRCodePNGURL(Long
								.parseLong(row.get("qrcode_id").toString())));
						newsList.add(newsReply);
					}

					writeQueryLotteryImageToUser(newsList); // 回复
					return null;

				}
			}

		}
		
		writeToUser(reply);
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		return true;
	}

}
